package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.UserMapper;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping("/login1")
    public ResponseBodyMessage<User> login1(@RequestParam String username,@RequestParam String password,
                                            HttpServletRequest requset){
        User userLogin = new User();
        userLogin.setUsername(username);
        userLogin.setPassword(password);

        User user = userMapper.login(userLogin);
        if(user == null) {
            System.out.println("登陆成功！");
            return new ResponseBodyMessage<>(-1, "登陆失败！",userLogin);
        } else {
            System.out.println("登陆成功");
            requset.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0, "登陆成功！",userLogin);
        }
    }

    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username,@RequestParam String password,
                                           HttpServletRequest requset) {
        User user = userMapper.selectByName(username);

        if(user == null) {
            System.out.println("登陆失败！");
            return new ResponseBodyMessage<>(-1, "用户名和密码错误！",user);
        } else {
            boolean flg = bCryptPasswordEncoder.matches(password, user.getPassword());
            if(!flg) {
                return new ResponseBodyMessage<>(-1,"用户名和密码错误！",user);
            }
            System.out.println("登陆成功");
            requset.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(0, "登陆成功！",user);
        }
    }

    @RequestMapping("/reg")
    public ResponseBodyMessage<User> register(@RequestParam String username,
                                              @RequestParam String password,
                                              HttpServletRequest request) {

        String newPassword = bCryptPasswordEncoder.encode(password);
        User user = new User();
        //通过用户名来查询，看能否可以查询到
        user.setUsername(username);
        user.setPassword(newPassword);
        User u = userMapper.selectByName(username);
        if(u != null){
            //用户名已存在，不能注册
            return new ResponseBodyMessage<>(-1,"注册失败！用户名已存在",user);

        }
        int ret = userMapper.insertUser(user);

        if(ret == 1){
            System.out.println("注册成功");
            return new ResponseBodyMessage<>(0,"注册成功！",user);
        }else{
            System.out.println("注册失败");
            //登录失败，返回响应，将返回类型改成自定义的响应类型
            return new ResponseBodyMessage<>(-1,"注册失败！",user);
        }
    }
}
